package org.testmyapp.tasks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.testmyapp.Pair;

public class ExampleTaskAUI extends GeneralUI implements TaskUI {

	public static String[] pages = { "PageFirst", "PageSecond" };
	
	public static List<Pair<String, String>> formularPageDef = new ArrayList<Pair<String,String>>() {{
		add(new Pair<String, String>("PageFirst","FormularA"));
		add(new Pair<String, String>("PageFirst","FormularB"));
		add(new Pair<String, String>("PageSecond","FormularC"));
	}};
	
	public static List<Pair<String, String>> changePageDef = new ArrayList<Pair<String,String>>() {{
		add(new Pair<String, String>("PageFirst","travelToPageSecond"));
		add(new Pair<String, String>("PageSecond","travelToPageFirst"));
	}};
	
	public static String[] actions = { "FormASend", "FormBSend", "FormCSend", "travelToPageSecond", "travelToPageFirst" };
	
	public static List<Pair<String, String>> actionForm = new ArrayList<Pair<String,String>>() {{
		add(new Pair<String, String>("FormularA","FormASend"));
		add(new Pair<String, String>("FormularB","FormBSend"));
	}};
	
	public static Map<String,String> pageActionNav = new HashMap<String, String>() {/**
		 * 
		 */
		private static final long serialVersionUID = 3799962021774833256L;

	{
		put("travelToPageFirst", "PageFirst");
		put("travelToPageSecond", "PageSecond");
	}};
	
	public Map<String, String> navMessages = new HashMap<String, String>() {/**
		 * 
		 */
		private static final long serialVersionUID = -4872171807327581801L;

	{
		put("travelToPageFirst", "Zur Seite 1");
		put("travelToPageSecond", "Zur Seite 2");
		
	}};
	
	
	
	// State of UI: Current Page (First) Last Succesfully triggered Action (Second)
	Pair<String, String> state = new Pair<String, String>();
	String old = "";
	
	public String createUIElementsForUserTask(String formularAktion, String fehlerMeldung, Map<String, Object> vars) {
		this.vars = vars;
		StringBuilder htmlResult = new StringBuilder();
		if (fehlerMeldung != null && !fehlerMeldung.equals("")) {
			old += "Fehler: " + fehlerMeldung;
			return old;
		}
		else {
			List<String> formularsForPage = new ArrayList<String>();
			String page = null;
			if (formularAktion==null) {
				page = "PageFirst";
			} else {
				page = (pageActionNav.get(formularAktion) != null) ? pageActionNav.get(formularAktion) : state.getFirst();				
			}
			for (Pair<String, String> pageForm : formularPageDef) {
				if (pageForm.getFirst().equals(page)) {
					formularsForPage.add(pageForm.getSecond());
				}
			}
			for (String form : formularsForPage) {
				String action = null;
				for (Pair<String, String> actual : actionForm) {
					if (actual.getFirst().equals(form)) {
						action = actual.getSecond();
					}
				}
				htmlResult.append(getFormularHTMLData(form, action));
			}
			for (Pair<String, String> pageForm : changePageDef) {
				if (pageForm.getFirst().equals(page)) {
					String actionNavSite = pageForm.getSecond();
					htmlResult.append(FormularCollection.changePage(navMessages.get(actionNavSite), actionNavSite));
				}
			}
			state.setFirst(page);
			state.setSecond(formularAktion);
		}
		
		htmlResult.append(FormularCollection.completeTask());
		String htmlOut = htmlResult.toString();
		old = htmlOut;
		return htmlOut;
	}

}
